package azcat.planner.materials;

import java.util.ArrayList;
import java.io.*;

import javax.swing.JTextField;

import azcat.planner.gui.NeededMaterials;

public class MaterialsManager implements java.io.Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4405091183961555698L;
	private ArrayList<Material> neededMaterials;// contains the objects for each selected materials and all of their information	
	private NeededMaterials nm;
	private NeededMaterialsEstimator nme;
	private String []alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
	private int index =0;
	private ArrayList<String> tobeSaved = new ArrayList<String>();

	
	public MaterialsManager(NeededMaterials nm){
		this.nm = nm;
		nme = new NeededMaterialsEstimator();
		neededMaterials = new ArrayList<Material>();
		
	}
	public ArrayList<String> getToBeSaved(){
		return this.tobeSaved;
	}
	
	public ArrayList<Material> getNeededMaterialsList(){
		return this.neededMaterials;
	}
	private void callEstimations(){
		ArrayList<ArrayList<JTextField>> rows = nm.getRows();
		for(int i = 0; i < rows.size(); i++){
			String s="row " + i;
			//get the values form the text boxes and estimate the pieces per block
			//get the first material in the list, set the value pieces per block from the following result( call the estimator method by reading the dimensions of the boxes) 
			neededMaterials.get(i).setPiecesxBlock(nme.estimatePiecesPerBlock(Float.parseFloat(rows.get(i).get(0).getText()),
					Float.parseFloat(rows.get(i).get(1).getText()), Float.parseFloat(rows.get(i).get(2).getText()), neededMaterials.get(i)));
			neededMaterials.get(i).setTotalComps(nme.estimateTotalComponents(Integer.parseInt(rows.get(i).get(3).getText()), Integer.parseInt(rows.get(i).get(4).getText())));
			neededMaterials.get(i).setAmountNeeded(nme.estimateBlocksNeeded(neededMaterials.get(i)));	
			
			rows.get(i).get(5).setText(neededMaterials.get(i).getPiecesxBlock()+"");
			rows.get(i).get(6).setText(neededMaterials.get(i).getAmountNeeded()+"");
			rows.get(i).get(7).setText(neededMaterials.get(i).getTotalComps()+"");
			
			s = "row "+i+","+ neededMaterials.get(i).getMaterialType()+"," +neededMaterials.get(i).getSize()+"," +rows.get(i).get(0).getText() +"," + rows.get(i).get(1).getText() +"," + rows.get(i).get(2).getText() +"," + rows.get(i).get(3).getText() +"," +
					rows.get(i).get(4).getText() +"," + rows.get(i).get(5).getText() +"," + rows.get(i).get(6).getText() + ","+ rows.get(i).get(7).getText();
			tobeSaved.add(s);
		}		
	}
	/**
	 * Gets the data of the selected materials and creates the objects. The created objects
	 * are stored in an ArrayList
	 * @param materials
	 */
	public void getMaterialData(ArrayList<String> materials){		
		for (int i = 0; i < materials.size(); i+=2){
			readMaterials(materials.get(i), materials.get(i+1));//because combobox selection is passed in pairs			
			
		}
		callEstimations();
//		for(int i = 0; i< neededMaterials.size(); i++){     
//        	System.out.println(neededMaterials.get(i).getMaterialType());        	
//        }
//		displayData();
	}
	/**
	 * This Method is used to simulate the retrieval of the database
	 */
	private void readMaterials(String type, String size) {
		String[] info;
		BufferedReader inputStream = null;
		try {
            inputStream = new BufferedReader(new FileReader("materials.csv"));
            String l;
            while ((l = inputStream.readLine()) != null) {
                info = l.split(",");
                if(info[0].equals(type) && info[1].equals(size)){
                	createMaterialObject(type, info, size);
                }
            }            
		}
		catch(IOException e){	}
		finally {
            if (inputStream != null) {
                try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
		}
	}
	/**
	 * creates the material object based on the type
	 * Different materials need to have a null value`
	 * @param type of the material
	 * @param info from the material that is stored in the document
	 */
	private void createMaterialObject(String type, String[] info, String size){
		Material m;
		if(type.equals("Placas de Polietileno")){
			m = new Material(type, Float.parseFloat(info[2]), Float.parseFloat(info[3]), Float.parseFloat(info[4]), Float.parseFloat(info[5]),info[6],Integer.parseInt(info[7]));
			m.setComponentName(alphabet[index]);
			m.setPartNumber(nm.getPartNumber());
			m.setSize(size);
			index++;
			neededMaterials.add(m);
		}
		else if (type.equals("RPVC")){
			m = new Material(type, 1, Float.parseFloat(info[3]), 1, Float.parseFloat(info[5]),info[6],Integer.parseInt(info[7]));
			m.setComponentName(alphabet[index]);
			m.setPartNumber(nm.getPartNumber());
			m.setSize(size);
			index++;
			neededMaterials.add(m);
		}
		else if (type.equals("Crosslink")){
			m = new Material(type, Float.parseFloat(info[2]), Float.parseFloat(info[3]), Float.parseFloat(info[4]), Float.parseFloat(info[5]),info[6],Integer.parseInt(info[7]));
			m.setComponentName(alphabet[index]);
			m.setPartNumber(nm.getPartNumber());
			m.setSize(size);
			index++;
			neededMaterials.add(m);
		}
		else if (type.equals("ABS")){
			m = new Material(type, 0, Float.parseFloat(info[3]), Float.parseFloat(info[4]), Float.parseFloat(info[5]),info[6],Integer.parseInt(info[7]));
			m.setComponentName(alphabet[index]);
			m.setPartNumber(nm.getPartNumber());
			m.setSize(size);
			index++;
			neededMaterials.add(m);
		}
		else if (type.equals("Burbuja")){
			m = new Material(type, 0, Float.parseFloat(info[3]), Float.parseFloat(info[4]), Float.parseFloat(info[5]),info[6],Integer.parseInt(info[7]));
			m.setComponentName(alphabet[index]);
			m.setPartNumber(nm.getPartNumber());
			m.setSize(size);
			index++;
			neededMaterials.add(m);
		}
		else if (type.equals("Carton")){
			m = new Material(type, 0, Float.parseFloat(info[3]), Float.parseFloat(info[4]),Float.parseFloat(info[5]),info[6],Integer.parseInt(info[7]));
			m.setComponentName(alphabet[index]);
			m.setPartNumber(nm.getPartNumber());
			m.setSize(size);
			index++;
			neededMaterials.add(m);
		}
		else if (type.equals("Rollos de Foam")){
			m = new Material(type, 0, Float.parseFloat(info[3]), Float.parseFloat(info[4]), Float.parseFloat(info[5]),info[6],Integer.parseInt(info[7]));
			m.setComponentName(alphabet[index]);
			m.setPartNumber(nm.getPartNumber());
			m.setSize(size);
			index++;
			neededMaterials.add(m);
		}
		else if (type.equals("Plastico Corrugado")){
			m = new Material(type, 0, Float.parseFloat(info[3]), Float.parseFloat(info[4]), Float.parseFloat(info[5]),info[6],Integer.parseInt(info[7]));
			m.setComponentName(alphabet[index]);
			m.setPartNumber(nm.getPartNumber());
			m.setSize(size);
			index++;
			neededMaterials.add(m);
		}
	}
}
